package org.szgb.console.sys.line.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.line.entity.SysLine;
import org.szgb.console.sys.line.mapper.SysLineMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysLineService {
    private final SysLineMapper sysLineMapper;

    @Autowired
    public SysLineService(SysLineMapper sysLineMapper) {
        this.sysLineMapper = sysLineMapper;
    }

    @Transactional(readOnly = true)
    public SysLine get(String id) {
        return sysLineMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysLine> findList(SysLine sysLine) {
        if (sysLine != null && sysLine.getPageDto() != null) {
            Page<SysLine> page = new Page<>(sysLine.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysLine.setPage(page);
        }
        return sysLineMapper.findList(sysLine);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysLine sysLine) {
        return sysLineMapper.getCount(sysLine);
    }

    @Transactional()
    public void save(SysLine sysLine) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysLine.getId() == null || sysLine.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysLine.setCreatedBy(sysLine.getUpdatedBy());
            // 添加UUID
            sysLine.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysLineMapper.insert(sysLine);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysLineMapper.update(sysLine);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysLineMapper.delete(ids.split(","));
    }
}
